$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/sysrole1/list',
        datatype: "json",
        colModel: [			
			{ label: 'roleId', name: 'roleId', index: 'role_id', width: 50, key: true },
			{ label: '角色名称', name: 'roleName', index: 'role_name', width: 80 }, 			
			{ label: '备注', name: 'remark', index: 'remark', width: 80 }, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
            { label: '角色级别', name: 'roleRank',  width: 80 },
            { label: '角色类别', name: 'roleType', width: 80, formatter: function(value, options, row){
                return value === 0 ?
                    '<span class="label label-danger">用户</span>' :
                    '<span class="label label-success">管理员</span>';
            }},
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

//菜单树
var menu_ztree;
var menu_setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "menuId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    check:{
        enable:true,
        nocheckInherit:true
    }
};

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            roleName: null
        },
		showList: true,
		title: null,
		sysRole1: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.sysRole1 = {};
            vm.getMenuTree(null);
		},
		update: function () {
			var roleId = getSelectedRow();
			if(roleId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getMenuTree(roleId);
            //vm.getsysRole1(roleId)
		},
		saveOrUpdate: function () {

            //获取选择的菜单
            var nodes = menu_ztree.getCheckedNodes(true);
            var menuIdList = new Array();
            for(var i=0; i<nodes.length; i++) {
                menuIdList.push(nodes[i].menuId);
            }
            vm.sysRole1.menuIdList = menuIdList;
            //alert(vm.sysRole1.menuIdList);
            var url = vm.sysRole1.roleId == null ? "sys/sysrole1/save" : "sys/sysrole1/update";
            //alert(url);
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.sysRole1),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });

		},
		del: function () {
			var roleIds = getSelectedRows();
			if(roleIds == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/sysrole1/delete",
                        contentType: "application/json",
                        data: JSON.stringify(roleIds),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getsysRole1: function(roleId){
			$.get(baseURL + "sys/sysrole1/info/"+roleId, function(r){
                vm.sysRole1 = r.sysRole1;
                    //勾选角色所拥有的菜单
                    var menuIds = vm.sysRole1.menuIdList;
                    for(var i=0; i<menuIds.length; i++) {
                        var node = menu_ztree.getNodeByParam("menuId", menuIds[i]);
                        menu_ztree.checkNode(node, true, false);
                    }
            });
		},

        getMenuTree: function(roleId) {
            //加载菜单树
            $.get(baseURL + "sys/menu/list", function(r){
                menu_ztree = $.fn.zTree.init($("#menuTree"), menu_setting, r);
                //展开所有节点
                menu_ztree.expandAll(true);

                if(roleId != null){
                    vm.getsysRole1(roleId);
                }
            });
        },
		reload: function(event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'roleName': vm.q.roleName},
                page:page
            }).trigger("reloadGrid");
		}
	}
});